package cn.cpf.aop.annotation;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@Aspect
public class AopProxy {

    public static final String GLOBAL_EXECUTION = "execution(* cn.cpf.aop.annotation.AopObjectDemo.testAop(..))";

    @Before("execution(* cn.cpf.aop.annotation.AopObjectDemo.testAop(..))")
    public void beforetest(JoinPoint joinPoint) {
        System.out.println("###### before ----------------------------------");
        System.out.println(Arrays.toString(joinPoint.getArgs()));
    }


    @Before(GLOBAL_EXECUTION)
    public void beforetest() {
        System.out.println("###### before no args----------------------------------");
    }


    @After(GLOBAL_EXECUTION)
    public void after(JoinPoint joinPoint) {
        System.out.println("###### after ------------------------- " + joinPoint.getTarget().getClass().toString());
    }

    @AfterReturning(returning = "rst", value = GLOBAL_EXECUTION)
    public void afterReturn(JoinPoint joinPoint, Object rst) {
        System.out.println("###### afterReturn ------------------------- " + joinPoint.getTarget().getClass().toString());
        System.out.println("%%%%%%%%" + Arrays.toString(joinPoint.getArgs()));
        System.out.println("%%%%%%%%" + rst);
    }

    @AfterThrowing(throwing = "e", value = GLOBAL_EXECUTION)
    public void afterThrow(JoinPoint joinPoint, Throwable e) {
        System.out.println("###### afterReturn ------------------------- " + joinPoint.getTarget().getClass().toString());
        System.out.println("%%%%%%%%" + Arrays.toString(joinPoint.getArgs()));
        System.out.println("%%%%%%%%" + e.toString());
    }

    @Around(GLOBAL_EXECUTION)
    public void around(ProceedingJoinPoint pjp) {
        try {
            System.out.println("###### around ------------------------- " + Arrays.toString(pjp.getArgs()));
            pjp.proceed(new Object[]{"天台", "牛角面包"});
            System.out.println("###### end ------------------------- " + Arrays.toString(pjp.getArgs()));
            System.out.println("around  enenenenenenend");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }


}
